package me.bingzi.basketballcount;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button seconds_24;
	private TextView game_time, left_points, right_points;

	private int back_count = 1; // 连续按back键次数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		MyClickListener ClickListerner = new MyClickListener();

		findViewById(R.id.left_1).setOnClickListener(ClickListerner);
		findViewById(R.id.left_2).setOnClickListener(ClickListerner);
		findViewById(R.id.left_3).setOnClickListener(ClickListerner);

		findViewById(R.id.right_1).setOnClickListener(ClickListerner);
		findViewById(R.id.right_2).setOnClickListener(ClickListerner);
		findViewById(R.id.right_3).setOnClickListener(ClickListerner);

		seconds_24 = (Button) findViewById(R.id.seconds_24);
		seconds_24.setText("Clear Points");
		seconds_24.setOnClickListener(ClickListerner);

		MyEditorActionListener EditorActionListener = new MyEditorActionListener();

		game_time = (TextView) findViewById(R.id.game_time);
		game_time.setText("00:00 00");
		left_points = (TextView) findViewById(R.id.left_points);
		left_points.setText("00");
		left_points.setOnEditorActionListener(EditorActionListener);
		right_points = (TextView) findViewById(R.id.right_points);
		right_points.setText("00");
		right_points.setOnEditorActionListener(EditorActionListener);

		// 点击空白处清除edit text焦点
		findViewById(R.id.shell_box).setOnClickListener(ClickListerner);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (back_count == 1) {
				Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_LONG)
						.show();
				back_count++;
			} else {
				back_count = 1;
				MainActivity.this.finish();
			}
		}

		return true;
	}

	public class MyClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			int vid = v.getId();

			switch (vid) {
			case R.id.left_1:
				this.plusPoints(left_points, 1);
				break;
			case R.id.left_2:
				this.plusPoints(left_points, 2);
				break;
			case R.id.left_3:
				this.plusPoints(left_points, 3);
				break;
			case R.id.right_1:
				this.plusPoints(right_points, 1);
				break;
			case R.id.right_2:
				this.plusPoints(right_points, 2);
				break;
			case R.id.right_3:
				this.plusPoints(right_points, 3);
				break;
			case R.id.seconds_24:
				this.clearPoints(left_points);
				this.clearPoints(right_points);
				break;
			default:
				left_points.clearFocus();
				right_points.clearFocus();
				break;
			}
		}

		public void plusPoints(TextView v, int p) {
			CharSequence c = v.getText();
			int i = Integer.valueOf(c.toString()).intValue() + p;

			if (i < 0 || i > 999) {
				return;
			}

			String s = "";
			if (i < 10) {
				s = "0";
			}
			s = s + String.valueOf(i);

			v.setText(s);
		}

		public void clearPoints(TextView v) {
			v.setText("00");
		}
	}

	public class MyEditorActionListener implements OnEditorActionListener {
		@Override
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			CharSequence text = v.getText();

			if (text.length() > 3) {
				Toast.makeText(MainActivity.this, "最大输入3位数字", Toast.LENGTH_LONG)
						.show();
				// 关闭软键盘
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				// 移除焦点
				v.clearFocus();
			}

			return true;
		}
	}
}
